package com.sat.wisdom.Fragments;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.sat.slidemenu.SlideMenu;
import com.sat.wisdom.Activities.MainActivity;
import com.sat.wisdom.Domain.NewsCategory;
import com.sat.wisdom.R;
import com.sat.wisdom.Views.Bases.BaseMenuDetailPage;
import com.sat.wisdom.Views.Impl.LeftMenu.InteractMenuDetailsPager;
import com.sat.wisdom.Views.Impl.LeftMenu.NewsMenuDetailsPager;
import com.sat.wisdom.Views.Impl.LeftMenu.PictureMenuDetailsPager;
import com.sat.wisdom.Views.Impl.LeftMenu.TopicMenuDetailsPager;
import com.sat.wisdom.Views.Impl.NewsCenterPage;

import java.util.ArrayList;

/**
 * Project:Wisdom
 * Package:com.sat.wisdom.Fragments
 * Created by 透野君爱打奶炮 on 2018-04-25 at 17:00
 */
public class LeftMenuFragment extends BaseFragment {

    //@ViewInject(R.id.lv_left_menu)
    private ListView lv_left_menu;
    private ArrayList<NewsCategory.Data> mNewsCategory;
    private int mCurrentPos;

    @Override
    public View initView() {
        View view = View.inflate(mActivity, R.layout.fragment_left_menu, null);
        lv_left_menu = view.findViewById(R.id.lv_left_menu);
        return view;
    }

    @Override
    public void initData() {

    }

    /**
     * 侧边栏菜单设置数据
     *
     * @param newsCategory
     */
    public void setMenuData(ArrayList<NewsCategory.Data> newsCategory) {
        //当前选中的位置归零
        mCurrentPos = 0;

        //更新页面
        mNewsCategory = newsCategory;
        final LeftMenuAdapter leftMenuAdapter = new LeftMenuAdapter();
        lv_left_menu.setAdapter(leftMenuAdapter);
        lv_left_menu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mCurrentPos = position;
                leftMenuAdapter.notifyDataSetChanged();
                setCurrentDetailPager(position);
                SlideMenu slide_menu = mActivity.findViewById(R.id.slide_menu);
                slide_menu.switchState();
            }
        });
    }

    private void setCurrentDetailPager(int position) {
        MainActivity activity = (MainActivity) mActivity;
        ContentFragment contentFragment = activity.getContentFragment();
        NewsCenterPage newsCenterPager = contentFragment.getNewsCenterPager();
        newsCenterPager.setCurrentDetailPager(position);
    }

    class LeftMenuAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return mNewsCategory.size();
        }

        @Override
        public NewsCategory.Data getItem(int position) {
            return mNewsCategory.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = View.inflate(mActivity, R.layout.left_menu_category, null);
            TextView tv = view.findViewById(R.id.tv_left_menu);
            tv.setText(getItem(position).title);

            if (mCurrentPos == position){
                tv.setEnabled(true);
            }else {
                tv.setEnabled(false);
            }

            return view;
        }
    }
}
